package com.heima.leetcode.practice;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @author 勾新杰
 * @version 1.0
 * @description: leetcode 199. 二叉树的右视图
 * @date 2025/10/8 20:20
 */
public class E199 {

    private final List<Integer> result = new ArrayList<>();

    /**
     * 层序遍历
     *
     * @param root 根节点
     * @return 二叉树的右视图
     */
    public List<Integer> rightSideView(TreeNode root) {
        levelOrder(root);
        return result;
    }

    /**
     * 层序遍历
     *
     * @param root 根节点
     */
    @SuppressWarnings("all")
    private void levelOrder(TreeNode root) {
        LinkedList<TreeNode> queue = new LinkedList<>();
        if (root == null) return;
        queue.offer(root);
        while (!queue.isEmpty()) {
            int size = queue.size();
            for (int i = 0; i < size; i++) {
                TreeNode node = queue.poll();
                if (node.left != null) {
                    queue.offer(node.left);
                }
                if (node.right != null) {
                    queue.offer(node.right);
                }
                if (i == size - 1) result.add(node.val);
            }
        }
    }
}
